<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/9
 * Time: 21:24
 */

namespace app\api\service;
use app\api\model\Order as OrderModel;
use app\api\service\Order as OrderService;
use app\lib\enum\OrderStatusEnum;
use app\lib\exception\OrderException;
use app\lib\exception\TokenException;

class Pay
{
    private $orderId;
    private $orderNo;
    public function __construct($orderId)
    {
        if(!$orderId)
        {
            throw new Exception('订单号不能为空！');
        }
        $this -> orderId = $orderId;
    }
    //支付方法
    public function pay()
    {
        //1.0定单号是否存在
        //2.0定单号和用户是否匹配
        //3.0定单是否己经付款
        $this -> checkOrderValidateid();
        //库存量检测
        $order = new OrderService();
        $status = $order -> checkOrderStock($this -> orderId);
        if(!$status['pass']) return $status;
    }
    //检查定单是否合法
    public function checkOrderValidateid()
    {
        $order = OrderModel::get($this -> orderId);
         if(!$order) throw new OrderException(['msg' => '定单不存在!']);
         if(!Token::checkValUid($order -> user_id)) throw new TokenException(['msg' => '订单和用户不匹配!', 'errorCode' => 10003]);
         if($order -> status != OrderStatusEnum::UNPAID) throw new OrderException(['定单状态异常!']);
         $this -> orderNo = $order -> order_no;
         return true;
    }
}